package me.zhengjie.modules.bushutongji.rest;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.bushutongji.SynQueryDemo;
import me.zhengjie.modules.bushutongji.domain.Bushutongji;
import me.zhengjie.modules.bushutongji.service.BushutongjiService;
import me.zhengjie.modules.bushutongji.service.dto.BushutongjiQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
* @author xiexidong 
* @date 2019-07-24
*/
@Api(tags = "Bushutongji管理")
@RestController
@RequestMapping("api")
public class BushutongjiController {

    @Autowired
    private BushutongjiService bushutongjiService;

    @Log("查询Bushutongji")
    @ApiOperation(value = "查询Bushutongji")
    @GetMapping(value = "/bushutongji")
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_SELECT','kuguan','gongchengshi','caiwu','xiaoshou')")
    public ResponseEntity getBushutongjis(BushutongjiQueryCriteria criteria, Pageable pageable){

        String userName= SecurityUtils.getUsername();
        if(userName=="黄金万"||userName.equals("黄金万"))
        {
            criteria.setBushuren("黄金万");
        } else if(userName=="林业辉"||userName.equals("林业辉"))
        {
            criteria.setBushuren("林业辉");
        }else if(userName=="唐国洋"||userName.equals("唐国洋"))
        {
            criteria.setBushuren("唐国洋");
        }else if(userName=="闫国卫"||userName.equals("闫国卫"))
        {
            criteria.setBushuren("闫国卫");
        }else if(userName=="刘南燕"||userName.equals("刘南燕"))
        {
            criteria.setHetongfuzeren("刘南燕");
        }else if(userName=="蓝宝意"||userName.equals("蓝宝意"))
        {
            criteria.setHetongfuzeren("蓝宝意");
        }

        return new ResponseEntity(bushutongjiService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增Bushutongji")
    @ApiOperation(value = "新增Bushutongji")
    @PostMapping(value = "/bushutongji")
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_CREATE','kuguan','gongchengshi','caiwu','xiaoshou')")
    public ResponseEntity create(@Validated @RequestBody Bushutongji resources){
        return new ResponseEntity(bushutongjiService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改Bushutongji")
    @ApiOperation(value = "修改Bushutongji")
    @PutMapping(value = "/bushutongji")
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_EDIT','kuguan','gongchengshi','caiwu','xiaoshou')")
    public ResponseEntity update(@Validated @RequestBody Bushutongji resources){
        bushutongjiService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除Bushutongji")
    @ApiOperation(value = "删除Bushutongji")
    @DeleteMapping(value = "/bushutongji/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_DELETE','kuguan','gongchengshi','caiwu','xiaoshou')")
    public ResponseEntity delete(@PathVariable Integer id){
        bushutongjiService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("查询快递信息")
    @ApiOperation(value = "查询快递信息")
    @PostMapping(value = "/kuaidi/{code}" ,produces = {"application/text;charset=UTF-8"})
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_DELETE','kuguan','gongchengshi','caiwu','xiaoshou')")
    public String kuaidi(@PathVariable String code){
        String key = "hcLIiNsH7630";				//贵司的授权key
        String customer = "DEA862DDBDBF0081273E58659647A3BC";			//贵司的查询公司编号
        String com = "";			//快递公司编码
        String num = code;	//快递单号
        String phone = "";				//手机号码后四位
        String from = "";				//出发地
        String to = "";					//目的地
        int resultv2 = 0;				//开启行政规划解析
        System.out.println("11111");
        SynQueryDemo demo = new SynQueryDemo(key, customer);
        com=demo.getG(code);
        String result = demo.synQueryData(com, num, phone, from, to, resultv2);
       return  result  ;
        //return new ResponseJSONP(HttpStatus.OK);
    }

    @Log("加载单位下拉框")
    @ApiOperation(value = "加载单位下拉框")
    @PostMapping(value = "/sel/danwei")
    @PreAuthorize("hasAnyRole('ADMIN','BUSHUTONGJI_ALL','BUSHUTONGJI_DELETE','kuguan','gongchengshi','caiwu','xiaoshou')")
    public  Map<String,String> findDanwei(){
        Map<String,String> mapDanwei= new HashMap<String,String>();
       List<String > listTmp=bushutongjiService.findDanwei();
        for(int i=0;i<listTmp.size();i++)
        {
            mapDanwei.put("danwei",listTmp.get(i));
        }
        return   mapDanwei;
        //return new ResponseJSONP(HttpStatus.OK);
    }


}